package com.kqkj.dyrule.engine.util;

import com.kqkj.dyrule.engine.beans.EventParam;
import com.kqkj.dyrule.engine.beans.EventSequenceParam;
import com.kqkj.dyrule.engine.beans.RuleConditions;

import java.util.Arrays;
import java.util.HashMap;

/**
 * 规则模拟生成器
 *  规则：
 *        触发事件： K事件，事件属性（P2=V1）
 *        画像属性条件：tag87=v2 tag26=v1   hbase
 *        行为次数条件：2022-04-17-当前  事件C[p6=v8,p12=v12] 做过>=2次 clickhouse
 */
public class RuleMonitor {
    public static RuleConditions getRule(){
        RuleConditions ruleConditions = new RuleConditions();
        ruleConditions.setRuleId("rule_001");
        //触发
        HashMap<String, String> map1 = new HashMap<>();
        //map1.put("p2","v1");
        EventParam eventParam = new EventParam("K", map1, 0, -1, -1,null);
        ruleConditions.setTriggerEvent(eventParam);
        //用户画像
        HashMap<String, String> map2 = new HashMap<>();
        map2.put("tag87","v2");
        map2.put("tag26","v1");
        ruleConditions.setUserProfileConditions(map2);
        //行为次数
        HashMap<String, String> map3 = new HashMap<>();

        long startTime = 1650124800000L;
        long endTime = Long.MAX_VALUE;
        String sql = "select count(1) from zenniu_detail " +
                "where eventId='K' and properties['p6']='v1' and properties['p1']='v1' " +
                "and deviceId=? and timeStamp between ? and ? ";
        EventParam eventParam1 = new EventParam("K", map3, 2, startTime, endTime,sql);
        ruleConditions.setActionCountConditions(Arrays.asList(eventParam1));

        HashMap<String, String> m1 = new HashMap<>();
        m1.put("p3","v2");
        EventParam e1 = new EventParam("A", m1, -1, startTime, endTime, null);
        HashMap<String, String> m2 = new HashMap<>();
        m2.put("p1","v1");
        EventParam e2 = new EventParam("C", m1, -1, startTime, endTime, null);
        String seqSql = "select sequenceMatch('.*(?1).*(?2).*')(toDateTime(timeStamp),eventId='A',eventId='C') as isMatch2,\n" +
                "sequenceMatch('.*(?1).*')(toDateTime(timeStamp),eventId='A',eventId='C') as isMatch1 \n" +
                "from zenniu_detail where ((properties['p3']='v2' and eventId='A') or (properties['p1']='v1' and eventId='C')) and deviceId=?\n" +
                " and timeStamp between ? and ?" ;

        EventSequenceParam eventSequenceParam = new EventSequenceParam("rule_001",Arrays.asList(e1,e2),startTime,endTime,seqSql);

        ruleConditions.setActionSequenceCondition(Arrays.asList(eventSequenceParam));
        return ruleConditions;
    }
}
